//lolin32 lite
//19-sda
//23-scl


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

#define inputPIN 12 //TSIC-716
#define powerPIN 18 //TSIC-716

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C 


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

BluetoothSerial SerialBT;

float temperature;
volatile bool DataReady = false;
volatile uint32_t time1 = 0, time2 = 0, timer1;
volatile uint16_t digitalData = 0;
volatile uint8_t i = 0;

IRAM_ATTR void isr1() { //Длительноность высокого уровня

  detachInterrupt(inputPIN);
  time1 = micros();
  if (time1 > time2) {
    timer1 = time1 - time2;
  }
  else {
    timer1 = 1 + (~time2) + time1;
  }

  if (timer1 > 500) {
    DataReady = false;
    i = 0;
    digitalData = 0;
  }
  else if (timer1 < 30) {
    digitalData = digitalData << 1;
    i++;
  }
  else if ((timer1 > 60) && (timer1 < 100) ) {
    digitalData = digitalData << 1 | 1;
    i++;
  }
  if (i == 16 ) {
    DataReady = true;
    digitalWrite(powerPIN, LOW);
  }
  attachInterrupt(inputPIN, isr2, CHANGE);
}
IRAM_ATTR void  isr2() { //Длительноность низкого уровня

  detachInterrupt(inputPIN);
  time2 = micros();
  attachInterrupt(inputPIN, isr1, FALLING);
}

void setup() {
  Serial.begin(115200);
  pinMode(inputPIN, INPUT);
  pinMode(powerPIN, OUTPUT);
  SerialBT.begin("ESP32thermometer"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  attachInterrupt(inputPIN, isr1, FALLING);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(1000);
  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(4);             // Draw 4X-scale text
  display.setTextColor(SSD1306_WHITE);
  delay(1500);
  digitalWrite(powerPIN, HIGH);
}

void loop() {
  if (DataReady) {
    noInterrupts();
    DataReady = false;
    temperature = (float(digitalData) / 16383.0) * 82.0 - 16.0; // для датчика -16/+66 градусов
    //temperature = (float(digitalData) / 16383.0) * 70.0 - 10.0; // для стандартного датчика -10/+60 градусов
    interrupts();
    Serial.println(temperature);
    //Serial.println(digitalData, BIN);
    SerialBT.println(temperature);// SerialBT.print(" / "); SerialBT.println(digitalData);
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print(temperature);
    display.display();
    delay(1500);
    digitalWrite(powerPIN, HIGH);

  }
}
